package controller;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import dao.ITopicDao;
import entity.Topic;
import dao.impl.TopicDaoImpl;
import service.ITopicService;
import service.impl.TopicService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class EditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        SmartUpload sm=new SmartUpload();
        sm.initialize(this.getServletConfig(),request,response);
        sm.setCharset("UTF-8");
        try {
            sm.upload();
            sm.save("fil");
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
        Request srt=sm.getRequest();
        Integer id= Integer.valueOf(request.getParameter("mid"));
        String title=srt.getParameter("title");
        String txt=srt.getParameter("mcontent");
        String pic=sm.getFiles().getFile(0).getFileName();
        Topic tc=new Topic(title,txt,pic);
        ITopicService itd=new TopicService();
        int b=itd.updPublish(tc,id);
        if (b>0){

            out.print("<script>alert('修改成功');location.href='pag';</script>");
        }else {
            out.print("<script>alert('修改失败');location.href='pag';</script>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
